package com.jxstjh.conch.handler.handler;

import com.jxstjh.conch.common.domain.AnchorInfo;
import com.jxstjh.conch.common.domain.TaskInfo;
import com.jxstjh.conch.common.enums.AnchorStateEnum;
import com.jxstjh.conch.support.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * 各渠道发送Handler
 */
public abstract class BaseMessageHandler implements MessageHandler {

    /**
     * 标识渠道的Code
     * 子类初始化的时候指定
     */
    protected Integer channelCode;

    @Autowired
    private MessageHandlerHolder messageHandlerHolder;

    @Autowired
    private LogUtil logUtil;

    /**
     * 初始化渠道与Handler的映射关系
     */
    @PostConstruct
    private void init() {
        messageHandlerHolder.putMessageHandler(channelCode, this);
    }

    /**
     * 统一处理的handler接口
     */
    public abstract boolean handler(TaskInfo taskInfo);

    @Override
    public void doHandler(TaskInfo taskInfo) {
        if (handler(taskInfo)) {

            logUtil.print(AnchorInfo.builder().state(AnchorStateEnum.SEND_SUCCESS.getCode()).businessId(taskInfo.getBusinessId()).ids(taskInfo.getReceiver()).build());
            return;
        }
        logUtil.print(AnchorInfo.builder().state(AnchorStateEnum.SEND_FAIL.getCode()).businessId(taskInfo.getBusinessId()).ids(taskInfo.getReceiver()).build());
    }
}